﻿<cfsilent>
	<cfscript>
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		termId = event.getArg("TermID");
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		targetWeek = event.getArg("W");
		
		passArgs = structNew();
		structInsert( passArgs, "TSK", taskId );
		structInsert( passArgs, "TT", taskType );
		structInsert( passArgs, "TID", termId );
		structInsert( passArgs, "W", targetWeek );
		
		switch(taskType){
			case "C":
				sql = "SELECT 
							t_course.course_name, 
							a.task_week, 
							t_teacher.tch_name, 
							t_teacher.tch_id, 
							a.task_period, 
							a.task_chooesd 
						FROM 
							t_common_task a 
							INNER JOIN t_course ON t_course.cid = a.cid 
							INNER JOIN t_teacher ON t_teacher.tch_id = a.tch_id 
						WHERE 
							a.tsk_id = :taskId ";
				break;

			case "P":
				sql = "SELECT 
							t_course.course_name, 
							a.task_week, 
							t_teacher.tch_name, 
							t_teacher.tch_id, 
							a.task_period, 
							a.task_chooesd 
						FROM 
							t_task a 
							INNER JOIN t_course ON t_course.cid = a.cid 
							INNER JOIN t_teacher ON t_teacher.tch_id = a.tch_id 
						WHERE 
							a.tsk_id = :taskId ";
				break;
			
			case "R":
				sql = "SELECT 
							t_course.course_name, 
							a.task_week, 
							t_teacher.tch_name, 
							t_teacher.tch_id, 
							a.task_period, 
							a.task_chooesd 
						FROM 
							t_reopen_task a 
							INNER JOIN t_course ON t_course.cid = a.cid 
							INNER JOIN t_teacher ON t_teacher.tch_id = a.tch_id 
						WHERE 
							a.tsk_id = :taskId ";
				break;
			
			case "T":
				sql = "SELECT 
							t_course.course_name, 
							a.task_week, 
							t_teacher.tch_name, 
							t_teacher.tch_id, 
							a.task_period, 
							a.task_chooesd 
						FROM 
							t_sport_task a 
							INNER JOIN t_course ON t_course.cid = a.cid 
							INNER JOIN t_teacher ON t_teacher.tch_id = a.tch_id 
						WHERE 
							a.tsk_id = :taskId ";
				break;
		}
		
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
		rs_task = queryObj.execute( sql=sql ).getResult();
		
		teacherId = rs_task.tch_id;

		event.setArg("pageTitle", "教学任务安排 - 补课向导 - " & rs_task.course_name );

		/* 排课周学时 */
		taskPeriod = rs_task.task_period;		

		timeTable = structNew();


		/* 计算可用补课时间 */
		checkTable = arrayNew(2);
		
		for(weekDay=1; weekDay lte 7; weekDay++){
			for(lecture=1; lecture lte 6; lecture++){
				checkTable[weekDay][lecture] = 1;
			}
		}

		/* 按人检测补课冲突 */
		sql = "SELECT 
					a.tasktime, 
					COUNT(DISTINCT d.stu_id) countStu 
				FROM 
					t_task_timetable a, 
					t_student_course d, 
					t_task_arrange c 
				WHERE 
					c.task_id = a.task_id 
					AND 
					c.task_id = d.task_id 
					AND 
					C.term_id = :termId 
					AND 
					SUBSTR(a.widx, " &targetWeek& ", 1) = '1' 
					AND 
					EXISTS(
						 SELECT 1 FROM t_student_course e 
						 WHERE 
								e.stu_id = d.stu_id 
								AND 
								e.task_id = :taskId 
					) 
				GROUP BY 
					a.widx, a.tasktime ";
					
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
		rs_confilict = queryObj.execute( sql=sql ).getResult();
	
		for ( i=1; i LTE rs_confilict.recordCount; i++){
			
			lec = rs_confilict["tasktime"][i];
			sVal = rs_confilict["countStu"][i];
			
			switch ( listGetAt(lec, 3, '-@') ) {
							
				case "2":

					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@");

					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@"))] = -2;
					}
					
					break;
							
				case "3":
					
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@");
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@"))] = -2;
					}
								
					/* 若第五节讲3节 则冲突计算至第六讲 */
					if ( listGetAt(lec, 2, "-@") eq 5 ) {
									
						sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") + 1;
						
						if (structKeyExists(timeTable, sKey)) {
							sVal = timeTable[sKey] + rs_confilict["countStu"][i];
							structInsert(timeTable, sKey, sVal, true);
						}
						else {
							structInsert(timeTable, sKey, sVal);
						}
						
						/* 如果冲突学生超过10% 则不允许补课 */
						if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
							checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -2;
						}
									
					} 
								
					break;
							
				case "4":
				case "5":
								
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@");
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -2;
					}
					
					/* 4/5节连排 则冲突延续一次 */
								
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") + 1;
					sVal = rs_confilict["countStu"][i];
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}

					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -2;
					}
					
					break;
							
				case "8":
								
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@");

					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@"))] = -2;
					}

								
					/* 8节连排 则冲突延续3次 */
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") + 1;
					sVal = rs_confilict["countStu"][i];
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -2;
					}
					
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") + 2;
					sVal = rs_confilict["countStu"][i];
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+2)] = -2;
					}
					
					sKey = "CNF-S" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") + 3;
					sVal = rs_confilict["countStu"][i];
						
					if (structKeyExists(timeTable, sKey)) {
						sVal = timeTable[sKey] + rs_confilict["countStu"][i];
						structInsert(timeTable, sKey, sVal, true);
					}
					else {
						structInsert(timeTable, sKey, sVal);
					}
					
					/* 如果冲突学生超过10% 则不允许补课 */
					if ( timeTable[sKey] / rs_task.task_chooesd gt 0.1 ) {
						checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+3)] = -2;
					}

					break;
							
							
			}
			
		}
		
		
		/* 教学班上课时间冲突 */
		sql = "SELECT 
				    t_task_timetable.tasktime, 
				    CASE WHEN t_task_timetable.taskclsroom IS NULL THEN '-' ELSE t_task_timetable.taskclsroom END taskclsroom 
				FROM 
					t_task_timetable 
					INNER JOIN t_task_arrange ON t_task_arrange.task_id = t_task_timetable.task_id 
				WHERE 
					t_task_timetable.task_id = :taskId 
					AND 
					SUBSTR(t_task_timetable.widx, " &targetWeek& ", 1) = '1' 
					AND 
					t_task_arrange.term_id = :termId ";
					
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
		rs_lecutre = queryObj.execute( sql=sql ).getResult();
	
		for ( i=1; i LTE rs_lecutre.recordCount; i++){
			
			lec = rs_lecutre["tasktime"][i];
			room = rs_lecutre["taskclsroom"][i];
			
			structInsert( timeTable, "LEC-W" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , rs_task.task_week );
			structInsert( timeTable, "LEC-C" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , rs_task.course_name );
			structInsert( timeTable, "LEC-T" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , listGetAt(lec, 3, "-@") );
			
			if ( room neq "-" ) {
				structInsert( timeTable, "LEC-R" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , room );
			}

			checkTable[listGetAt(lec, 1, "-@")][listGetAt(lec, 2, "-@")] = -1;
					
			/* 晚上第5讲安排了3节 则第六讲不可用 */
			if ( listGetAt(lec, 3, "-@") eq 3 and listGetAt(lec, 2, "-@") eq 5 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -1;
			}
			
			/* 4/5节连排 */
			if ( listGetAt(lec, 3, "-@") eq 4 or listGetAt(lec, 3, "-@") eq 5 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -1;
			}
			
			/* 8节连排 */
			if ( listGetAt(lec, 3, "-@") eq 8 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -1;
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+2)] = -1;
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+3)] = -1;
			}
				
		}
		
		/* 教师自己的上课时间冲突 */
		sql = "SELECT 
					t_teacher_timetable.tasktime, 
					CASE WHEN t_teacher_timetable.taskclsroom IS NULL THEN '-' ELSE t_teacher_timetable.taskclsroom END taskclsroom, 
					t_course.course_name, 
					t_teacher_timetable.task_week    
				FROM 
					t_teacher_timetable 
					INNER JOIN t_course ON t_course.cid = t_teacher_timetable.cid 
				WHERE 
					t_teacher_timetable.tch_id = :teacherId 
					AND 
					SUBSTR(t_teacher_timetable.widx, " &targetWeek& ", 1) = '1' 
					AND 
					t_teacher_timetable.term_id = :termId ";
					
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		rs_teacher = queryObj.execute( sql=sql ).getResult();
		
		for ( i=1; i LTE rs_teacher.recordCount; i++){
			
			lec = rs_teacher["tasktime"][i];
			room = rs_teacher["taskclsroom"][i];
			course = rs_teacher["course_name"][i];
			weekstr = rs_teacher["task_week"][i];

			structInsert( timeTable, "LEC-W" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , weekstr, true );			
			structInsert( timeTable, "LEC-C" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , course, true );			
			structInsert( timeTable, "LEC-T" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , listGetAt(lec, 3, "-@"), true );
			
			if ( room neq "-" ) {
				structInsert( timeTable, "LEC-R" & listGetAt(lec, 1, "-@") & listGetAt(lec, 2, "-@") , room, true );
			}
			
			checkTable[listGetAt(lec, 1, "-@")][listGetAt(lec, 2, "-@")] = -3;
					
			/* 晚上第5讲安排了3节 则第六讲不可用 */
			if ( listGetAt(lec, 3, "-@") eq 3 and listGetAt(lec, 2, "-@") eq 5 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -3;
			}
			
			/* 4/5节连排 */
			if ( listGetAt(lec, 3, "-@") eq 4 or listGetAt(lec, 3, "-@") eq 5 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -3;
			}
			
			/* 8节连排 */
			if ( listGetAt(lec, 3, "-@") eq 8 ) {
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+1)] = -3;
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+2)] = -3;
				checkTable[listGetAt(lec, 1, "-@")][(listGetAt(lec, 2, "-@")+3)] = -3;
			}
				
		}
	
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('taskTempChange')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">临时性调停课</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 card"></i>临时补课向导 <cfoutput>#rs_task.course_name#</cfoutput>-<cfoutput>#rs_task.tch_name#</cfoutput> 
				</h2>
			</div>
		</div>
	</div>
</div>

<!--- 加载导航菜单 --->
<!-- stepMenu-->
<cfoutput>#event.getArg("stepMenu")#</cfoutput>
<!--/stepMenu-->

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="summary">
				<table class="UICourseTable">
					<thead>
						<tr>
							<td colspan="2" width="2%"></td>
							<td width="14%">星期一</td>
							<td width="14%">星期二</td>
							<td width="14%">星期三</td>
							<td width="14%">星期四</td>
							<td width="14%">星期五</td>
							<td width="14%">星期六</td>
							<td width="14%">星期天</td>
						</tr>
					</thead>
					<tbody>
						<cfloop from="1" to="6" index="lec">
							<tr>
								<cfswitch expression="#lec#">
									<cfcase value="1">
									<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
									</cfcase>
									<cfcase value="3">
									<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
									</cfcase>
									<cfcase value="5">
									<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
									</cfcase>
								</cfswitch>
								<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
								<cfloop from="1" to="7" index="wkday">
									<td>
										
										<cfif checkTable[wkday][lec] eq 1>
											
											<cfset structInsert( passArgs, "wkday", wkday, true ) />
											<cfset structInsert( passArgs, "lec", lec, true ) />
											
											<cfif (lec eq 1) and (listFind(taskPeriod, "8", "+"))>
												
												<cfset structInsert( passArgs, "n", 8, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">8</a>
											
											<cfelseif (lec eq 1) and (listFind(taskPeriod, "5", "+"))>
								
												<cfset structInsert( passArgs, "n", 5, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">5</a>
							
											<cfelseif (lec eq 1 or lec eq 3) and listFind(taskPeriod, "4", "+")>
								
												<cfset structInsert( passArgs, "n", 4, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">4</a>
							
											<cfelseif (lec eq 5) and (listFind(taskPeriod, "3", "+") and listFind(taskPeriod, "2", "+"))>
								
												<cfset structInsert( passArgs, "n", 3, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">3</a>
												 / 
												<cfset structInsert( passArgs, "n", 2, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">2</a>
							
											<cfelseif (lec eq 2 or lec eq 5) and listFind(taskPeriod, "3", "+")>
												
												<cfset structInsert( passArgs, "n", 3, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">3</a>
							
											<cfelseif listFind(taskPeriod, "2", "+")>
												
												<cfset structInsert( passArgs, "n", 2, true ) />
												<a class="cellCount" href="<cfoutput>#buildURL('taskChangeWizardAppendClassroom', passArgs)#</cfoutput>">2</a>
											
											</cfif>
											
										</cfif>
										
										<cfif checkTable[wkday][lec] eq -2>
											<span>
												学生上课冲突
												<br/>
												(<cfoutput>#timeTable["CNF-S"&wkday&lec]#</cfoutput>)
												<span>
										</cfif>
										
										<cfif checkTable[wkday][lec] eq -3 or checkTable[wkday][lec] eq -1>
											<cfif structKeyExists(timeTable, "LEC-T" & wkday & lec)>
											
												<span>
												<cfoutput>#timeTable["LEC-C"&wkday&lec]#</cfoutput>
												<cfif structKeyExists(timeTable, "LEC-R" & wkday & lec)>
													<br/>
													<cfoutput>#timeTable["LEC-R"&wkday&lec]#</cfoutput>
												</cfif>
												<br/>
												<cfoutput>#timeTable["LEC-W"&wkday&lec]#</cfoutput>(<cfoutput>#timeTable["LEC-T"&wkday&lec]#</cfoutput>)
												</span>
											
											</cfif>
										</cfif>
										
										
									</td>
								</cfloop>
							</tr>
						</cfloop>
					</tbody>
				</table>
			</div>
		</li>
	</ul>
</div>